#!/bin/sh
# a APP名
# t docker版本
# n 检查空间
# e 项目环境
while getopts ":a:n:t:e:" opt; do
  case $opt in
  a)
    APP=$OPTARG
    ;;
  n)
    namespace=$OPTARG
    ;;
  t)
    tag=$OPTARG
    ;;
  e)
    ENV=$OPTARG
    ;;
  ?)
    echo "未知参数"
    ;;
  esac
done

echo "脚本参数 APP=${APP}  namespace=${namespace} tag=${tag} ENV=${ENV}"
cd /home/admin/k8s-app/$APP-$ENV/ADDP-INF/$ENV/$APP;
helm upgrade -f values.yaml --install --namespace=${n} --set image.tag=${tag} --set imagePullSecrets[0].name=registry-vpc.cn-shanghai.aliyuncs.com ${APP} .

health_check() {
  sleep 10
  echo "checking "
  while true; do
    status=$(kubectl get pods --namespace ${namespace} | grep ${APP} | grep 0/1)
    if [ "$status" ]; then
      sleep 1
      ((exptime++))
      echo -e "\rWait app to pass health check: $exptime..."
      if [ $exptime -gt 600 ]; then
        echo 'app start failed'
        exit 1
      fi
    else
      break
    fi
  done
  echo "check success"
}
health_check
